package pwd.allen.fastdfs.controller;

import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import pwd.allen.fastdfs.utils.FileDfsUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;

/**
 * 演示fastfds
 * @author allen
 * @date 2021/06/02
 */
@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil;

    /**
     * 文件上传
     */
    @ApiOperation(value = "上传文件", notes = "测试FastDFS文件上传")
    @PostMapping(value = "/uploadFile", headers = "content-type=multipart/form-data")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {

        String result = null;
        try {

            String path = fileDfsUtil.upload(file);
            if (StringUtils.hasLength(path)) {
                result = path;
            } else {
                result = "上传失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "服务异常";
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件上传
     */
    @ApiOperation(value = "上传图片", notes = "测试FastDFS文件上传")
    @PostMapping(value = "/uploadImg", headers = "content-type=multipart/form-data")
    public ResponseEntity<String> uploadImg(@RequestParam("file") MultipartFile file) {

        String result = null;
        try {

            String path = fileDfsUtil.uploadImg(file);
            if (StringUtils.hasLength(path)) {
                result = path;
            } else {
                result = "上传失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "服务异常";
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件删除
     */
    @GetMapping(value = "/deleteByPath")
    public ResponseEntity<String> deleteByPath(@RequestParam("path") String path) {
        fileDfsUtil.deleteFile(path);
        return ResponseEntity.ok("SUCCESS");
    }


    /**
     * TODO 下载的文本后面多了好多<0x00>
     *
     * @param path
     * @return
     */
    @GetMapping("/download")
    public ResponseEntity<String> hello(@RequestParam("path") String path) {
        String result = null;
        try {
            // 先查询出文件信息
            FileInfo fileInfo = fileDfsUtil.queryFileInfo(path);
            if (fileInfo == null) {
                result = "文件不存在";
            } else {
                byte[] bytes = fileDfsUtil.downloadFile(path);
                if (bytes == null) {
                    result = "文件不存在";
                } else {
                    FileOutputStream outputStream = new FileOutputStream(new File(System.getProperty("user.home") + "/Desktop/" + path.replaceAll(".*?([^/]*)$", "$1")));
                    outputStream.write(bytes);
                    outputStream.flush();
                    outputStream.close();
                    result = "下载成功：" + fileInfo.toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "服务异常";
        }
        return ResponseEntity.ok(result);
    }
}

